package org.dolphin.action.security;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.dolphin.base.constant.Status;
import org.dolphin.base.json.StatusBean;
import org.dolphin.service.SecurityControlService;
import org.dolphin.util.ExceptionHelper;
import com.opensymphony.xwork2.ActionSupport;

@ParentPackage(value = "custom-default")
@Namespace(value = "/userProfile")
@InterceptorRefs(value = {@InterceptorRef(value = "customStack")})
public class UserProfileAction extends ActionSupport {
  private static final long serialVersionUID = -3531393105840724729L;
  private StatusBean status;
  private SecurityControlService securityControlService;
  private String currentPassword;
  private String newPassword;
  private String confirmedNewPassword;

  @Action(value = "changePassword", results = @Result(name = "success", params = {"root", "status"}, type = "json"))
  public String changePassword() {
    boolean error = false;
    String msg = "";
    status = new StatusBean();
    try {
      getSecurityControlService()
          .changePassword(currentPassword, newPassword, confirmedNewPassword);
    } catch (Exception e) {
      error = true;
      msg = ExceptionHelper.getRootCause(e).getMessage();
    } finally {
      status.setStatus(error ? Status.ERROR : Status.SUCCESS);
      if (error && msg != null) {
        status.addError(msg);
      }
    }
    return SUCCESS;
  }

  public String getConfirmedNewPassword() {
    return confirmedNewPassword;
  }

  public String getCurrentPassword() {
    return currentPassword;
  }

  public String getNewPassword() {
    return newPassword;
  }

  public SecurityControlService getSecurityControlService() {
    return securityControlService;
  }

  public StatusBean getStatus() {
    return status;
  }

  public void setConfirmedNewPassword(String confirmedNewPassword) {
    this.confirmedNewPassword = confirmedNewPassword;
  }

  public void setCurrentPassword(String currentPassword) {
    this.currentPassword = currentPassword;
  }

  public void setNewPassword(String newPassword) {
    this.newPassword = newPassword;
  }

  public void setSecurityControlService(SecurityControlService securityControlService) {
    this.securityControlService = securityControlService;
  }

  public void setStatus(StatusBean status) {
    this.status = status;
  }

  @Action(value = "viewChangePassword", results = @Result(name = "success", location = "viewChangePassword.jsp"))
  public String viewChangePassword() {
    return SUCCESS;
  }
}
